<%@page import="com.mde.util.Validator"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.Scanner"%>
<%@ page import="java.sql.*" %>
<%@ page 
		language="java" 
		contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp" %>    

<!DOCTYPE html>
<html>
	<title>Points adding page</title>
	<head>
	 	<link rel="stylesheet" href="../styles.css">
		<script src="http://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU" 
				type="text/javascript"></script>
		<script type="text/javascript" src="../js/addPointsJs.js"></script>
	</head>
<body>
	<div id="map" style="width:800px; height:600px"></div>	
	<div id="route-add-content">
<%!	
	Statement stmt;
	Connection con;
	String url = Validator.dbURL;
	String query = "SELECT id, latitude, longitude, type, name FROM Points;";
%>	
<%
	Class.forName("com.mysql.jdbc.Driver");
	con = DriverManager.getConnection(url, Validator.dbUsername, Validator.dbUserpass); 
	stmt = con.createStatement();
	
	ResultSet rs = stmt.executeQuery(query);
%>
	<table class="hidden">
		<thead>
			<tr>
				<td>id</td>
				<td>name</td>
				<td>type</td>
				<td>latitude</td>
				<td>longitude</td>
			</tr>
		</thead>
		<tbody>

<%
	int count = 0;
	while(rs.next()){
		++count;
		int id  = rs.getInt("id");
		String pointName = rs.getString("name");
		String pointType = rs.getString("type");
		double latitude = rs.getDouble("latitude");
		double longitude = rs.getDouble("longitude");
%>		
			<tr>
				<td id="<%= count %>id"><%= id %></td>
				<td id="<%= count %>name"><%= pointName %></td>
				<td><%= pointType %></td>
				<td id="<%= count %>lat"><%= latitude %></td>
				<td id="<%= count %>lon"><%= longitude %></td>
			</tr>
<% 	}
	stmt.close();
	con.close();
%>
	        <tr><td id="count"><%= count %></td></tr>
		</tbody>
	</table>
	
	<div>
		<input id="showhide" type="button" onclick="showPoints()" value="Hide points"/>
	</div>
	
	<div class="form-div">
		<form action="addPointToDB.jsp" method="post">
			<table>
				<tr>
					<td>Name</td>
					<td><input onkeyup="checkName()" id="nameInput" name="name" type="text" placeholder="name" value=""/></td>
				</tr>
				<tr>
					<td>Type</td>
					<td><input onkeyup="checkType()" id="typeInput" name="type" type="text" placeholder="type" value=""/></td>
				</tr>
				<tr>
					<td>Latitude</td>
					<td><input id="latInput" name="latitude" type="text" placeholder="latitude" value=""/></td>
				</tr>
				<tr>
					<td>Longitude</td>
					<td><input id="lonInput" name="longitude" type="text" placeholder="longitude" value=""/></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="Add point"/></td>
				</tr>
			</table>
		</form>
		<form action="deletePoint.jsp">
			<input class="hidden" id="deleteId" name="deletePoint" type="text" value=""/>
			<input type="submit" value="Delete point"/>
		</form>
	</div>
	</div>	
</body>
</html>